package com.easyj.admin.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.easyj.admin.activiti.vo.ProcessDefVO;
import com.easyj.admin.activiti.vo.ProcessReqDTO;
import com.easyj.admin.sys.service.IProcessDefService;
import com.easyj.base.common.ajax.Result;
import com.easyj.base.common.page.Page;
import com.easyj.base.controller.BaseController;
import com.easyj.base.exception.ActivitiException;
import com.easyj.base.exception.EasyjException;



/**
         * 流程管理。
          * 一般情况下流程与业务（业务数据）紧密结合，只能微调流程，不能删除、新增。可按实际情况‘注解’其功能。 
 */
@RestController
@RequestMapping("/sys/process")
@PreAuthorize("hasPermission('process','process:all')")
public class ProcessController extends BaseController {

    @Autowired
    private IProcessDefService processDefService;


    /**
     * 获取流程定义列表
     *
     * @param processDefinition
     * @return
     */
    @PostMapping(value = "list")
    public Result list(@RequestBody ProcessDefVO filter) {
    	Page<ProcessDefVO> list= processDefService.findProcessDefinitionList(filter);
        return Result.Success(list);

    }
    /**
	* 导入更新流程/模型
	* @param model
	* @throws EasyjException
	*/
	@PostMapping(value = "impProcess")
		public Result impProcess(ProcessReqDTO model) throws EasyjException{
		processDefService.impProcess(model);
		return Result.Success("OK");
	}
    /**
     * 更新流程/模型
     * @param model
     * @throws EasyjException
     */
    @PostMapping(value = "updateProcess")
    public Result updateProcess(@RequestBody ProcessDefVO model) throws EasyjException{
    	processDefService.updateProcess(model);
    	return Result.Success("OK");
    }
    
    /**
	* 增加流程/模型
	* @param model
	* @throws EasyjException
	*/
	@PostMapping(value = "addProcess")
	public Result addProcess(@RequestBody ProcessDefVO model) throws EasyjException{
		processDefService.addProcess(model);
		return Result.Success("OK");
	}

	/**
	    *取得流程Model
	*
	* @param id
	* @return
	 * @throws ActivitiException 
	*/
    @RequestMapping(value = "getModelById", method = {RequestMethod.POST,RequestMethod.GET})
	public Result getModelById(@RequestParam(value = "id") String id) throws ActivitiException { 
	  return Result.Success(processDefService.getModel(id));
	}

    /**
                * 挂起流程流程定义
     *
     */

    @RequestMapping(value = "suspendById", method = {RequestMethod.POST,RequestMethod.GET})
    public Result suspendProcessById(@RequestParam(value = "id") String id) {
    	processDefService.suspendProcessById(id);
        return Result.Success("OK");
    }
    
    /**
              * 启动程流程定义
	*
	*/
	
	@RequestMapping(value = "activeById", method = {RequestMethod.POST,RequestMethod.GET})
	public Result activeProcessById(@RequestParam(value = "id") String id) {
		processDefService.activeProcessById(id);
		return Result.Success("OK");
	}
    
    

    /**
                  * 删除流程
     *
     * @param deploymentId
     * @return
     */
    @DeleteMapping("/{deploymentId}")
    public Result deleteDefinition(@PathVariable("deploymentId") String deploymentId) {
    	processDefService.deleteProcessByDeploymentId(deploymentId);
    	return Result.Success("OK");
    }
}
